// pages/restaurant/restaurant.js
const db=wx.cloud.database()
var util=require('../../utils/util.js')

import Dialog from '@vant/weapp/dialog/dialog';
Page({

  data: {

    show: false,
    isStatus1:false,
    isStatus2:false,
    isStatus3:false,
    isStatus4:false,
    Status1:[],
    Status2:[],
    Status3:[],
    Status4:[],
    cateItems:[
      {
        canteen_id:1,
        canteen_name:'田园餐厅',
      },
      {
        canteen_id:2,
        canteen_name:'龙山餐厅',
      }
    ],
    curNav:1,
    curIndex:0,
    meal:[
  
    ],
    student:{id:null,
             name:null ,
             phone:null },
    addOrderinfo:false,
      showmeal:{},
  },
  showPopup(e) {
   
    var meal=e.target.dataset.item
    this.setData({ show: true,
      showmeal:meal
     });
  },

  onClose() {
    this.setData({ show: false });
  },
  switchRightTab:function(e){
   
    let id = e.target.dataset.id,index=e.target.dataset.index;
    this.setData({
      curNav:id,
      curIndex:index,
      isStatus1:false,
      isStatus2:false,
      isStatus3:false,
      isStatus4:false,
      Status1:[],
      Status2:[],
      Status3:[],
      Status4:[],
    })
    db.collection("meal").where({
      "canteen.canteen_id": id,
      "meal_status":false
    }).get({
      success:res=>{
      this.setData({
        meal: res.data
      })
      this.mealType(id)
      }    
    })
  },
  

  reserve:function(e){
  

    console.log(this.data.student)
    var meal=e.target.dataset.item
    console.log(meal)
    var myDate = new Date();
    var Y=myDate.getFullYear();
    var M=myDate.getMonth()+1;    
    var D= myDate.getDate(); 
    var h = myDate.getHours();
    if (h<10){
      h="0"+h
    }
    var m =myDate.getMinutes();
    if (m<10){
      m="0"+m
    }
    var s =myDate.getSeconds();
    var date=Y+"/"+M+"/"+D+" "+h+":"+m+":"+s;
    console.log(date)
    //剩余饭菜减一
    var mealNum
    if(meal.meal_num>0){
       mealNum=meal.meal_num-1
    }else{
      mealNum=0
    }
   
     console.log(mealNum)
    // console.log(meal._id)
  
        db.collection("meal").doc(meal._id).update({
          data:{
            meal_num:mealNum
          }
        }).then(res=>{
          console.log(res)
          console.log("数目减少")
        })
    console.log(meal)
     
    if(this.data.student.id!=null){
        db.collection("order_info").add({
            data:{
              canteen:{
                id:meal.canteen.canteen_id,
                name:meal.canteen.canteen_name
              },
              meal:{
                id:meal._id,
                name:meal.meal_name,
                price:meal.meal_price
              },
              order_status:"0",
              order_time:date,
              student:this.data.student,
              window:{
                id:meal.window.window_id,
                name:meal.window.window_name
              }
            }

        })

    }
    this.getCanttenData();
 
    Dialog.confirm({
       title: '提示',
       message:this.data.student.id==null? '拒绝授权将以游客身份访问游客无法够饭':'恭喜您预订成功',
       confirmButtonText:this.data.student.id==null?'完善信息':'查看订单'
    })
      .then(() => {
      
        this.method();

      })
      .catch(() => {
        console.log("取消");
      });
  },

  method(){
    if(this.data.student.id==null){
      console.log("跳转到我的")
      wx.navigateTo({
        url: '../prefer/user/user'
      })
    }else{
      console.log("跳转到订单")
      wx.switchTab({
        url: '../reserve/reserve'
      })
    }

  },
  
  onSearch() {
    console.log("haha")
  },
  mealType(id){
    //是否有粥类
    db.collection("meal").where({
      "canteen.canteen_id":id,
      "meal_type":"1",
      "meal_status":false
    }).get({
      success:res=>{
      if(res.data.length>0){
        this.setData({
          isStatus1: true,
          Status1:res.data
        })
      }     
      }    
    })
    //是否有面食
    db.collection("meal").where({
      "canteen.canteen_id":id,
      "meal_type":"2",
      "meal_status":false
    }).get({
      success:res=>{
      if(res.data.length>0){
        this.setData({
          isStatus2: true,
          Status2:res.data
        })
      }     
      }    
    })
    //是否有米饭套餐
    db.collection("meal").where({
      "canteen.canteen_id":id,
      "meal_type":"3",
      "meal_status":false
    }).get({
      success:res=>{
      if(res.data.length>0){
        this.setData({
          isStatus3: true,
          Status3:res.data
        })
      }     
      }    
    })
    //是否有其他
    db.collection("meal").where({
      "canteen.canteen_id":id,
      "meal_type":"4",
      "meal_status":false
    }).get({
      success:res=>{
      if(res.data.length>0){
        this.setData({
          isStatus4: true,
          Status4:res.data
        })
      }     
      }    
    })

  },

  getCanttenData(){
    db.collection("canteen").get({
      success:res=>{  
        this.setData({
          cateItems:res.data,
          curNav:res.data[0]._id,    
        }) 
        db.collection("meal").where({
          "canteen.canteen_id": this.data.curNav
        }).get({
          success:res=>{    
          this.setData({
            meal: res.data
          })
          this.mealType(this.data.curNav)
         
          }    
        })
   
      }  
    })
  },
  
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: "student",
      success (res) {
        that.setData({
          student:{
            id:res.data.student_id,
            name:res.data.student_name ,
            phone:res.data.student_phone 
          }
        })
        console.log(res.data)
      }
    })
    console.log("test")

    this.getCanttenData();
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getStorage({
      key: "student",
      success (res) {
        that.setData({
          student:{
            id:res.data.student_id,
            name:res.data.student_name ,
            phone:res.data.student_phone 
          }
        })
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})